﻿//-----------------------------------------------------------------------
// <copyright file="LogManager.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Base.Log
{
    using System;
    using System.Collections.Generic;
    using TileTool.Log;

    /// <summary>
    /// Base implementation of the log manager.
    /// </summary>
    public class LogManager : ILogManager
    {
        #region Fields - Private

        /// <summary>
        /// List of output targets.
        /// </summary>
        private IList<ILogOutputTarget> targets = new List<ILogOutputTarget>();

        #endregion

        #region Events - Public

        /// <summary>
        /// Event triggered when there is a new log.
        /// </summary>
        public event LogEventHandler NewLog;

        #endregion

        #region Methods - Public

        /// <summary>
        /// Add the passed target to the set of targets log messages
        /// are output to.
        /// </summary>
        /// <param name="target">Target to add to the manager.</param>
        public void AddOutputTarget(ILogOutputTarget target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target", "Output target must be non-null.");
            }

            lock (this.targets)
            {
                int index = this.targets.IndexOf(target);
                if (index != -1)
                {
                    return;
                }

                this.targets.Add(target);
            }
        }

        /// <summary>
        /// Remove the passed target from the set of output targets.
        /// </summary>
        /// <param name="target">Target to remove from the manager.</param>
        public void RemoveOutputTarget(ILogOutputTarget target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target", "Output target must be non-null.");
            }

            lock (this.targets)
            {
                this.targets.Remove(target);
            }
        }

        /// <summary>
        /// Log a formatted string at the passed log level.
        /// </summary>
        /// <param name="level">Log level for the message.</param>
        /// <param name="group">Group this log message is associated with.</param>
        /// <param name="format">Message format.</param>
        /// <param name="args">Message args.</param>
        public void Log(LogLevel level, string group, string format, params object[] args)
        {
            string formattedMsg = string.Format(format, args);
            string finalMsg = string.Format("{0} - {1} - {2}{3}", level.ToString(), group, formattedMsg, System.Environment.NewLine);
            LogData data = new LogData(level, group, finalMsg);

            // $TODO - Make this distribute logs on a different thread. Have a background consumer
            // thread that takes new logs and pushes them out as it gets them.
            //
            // Might now be as useful since some crashes might not show up, though.
            lock (this.targets)
            {
                foreach (ILogOutputTarget target in this.targets)
                {
                    target.Log(data);
                }

                LogEventHandler temp = this.NewLog;
                if (temp != null)
                {
                    temp(data);
                }
            }
        }

        #endregion
    }
}
